<?php
require_once('Driver.php');

/**
 * Die Klasse für die Teams.
 * Erbt von der Fahrer-Klasse.
 *
 * @author Timo
 */
class Team extends Driver
{
    private $driver_count = 0;
    private $drivers      = array(); // Objekte
    private $driver_ids   = array(); // IDs

/**
 * Team Konstruktor
 * -> Anzahl der Fahrer wird benötigt.
 *
 * @author Timo
 */
    function Team($countOfDrivers = 0, $load = false)
    {
        $this->name         = 'Default Team';
        if($countOfDrivers > 0)
        {
            $this->driver_count = $countOfDrivers;
            // Fahrer initialisieren..
            for($i = 0; $i < $this->driver_count; $i++)
            {
                $this->drivers[] = new Driver();
            }
        }
        else
        {
            echo '<b>Important: Add drivers to the team before doing something else.</b>';

        }

        /**
         *Lädt die Statistiken für das Team.
         * Hinweis: Sofern man nichts mit ihnen vor hat,
         * sollte "$load" auf FALSE stehen.
         */
        if($load)
        {
            $this->stats = new DriverStatistics();
            $this->stats->Load();
        }
    }
 /**
 * Fügt dem Team einen Fahrer hinzu.
 * Optional: Fahrername
 *
 * @author Timo
 */
    public function AddDriver($name = 'New Driver')
    {
        try
        {
            $this->drivers[] = new Driver();
            if(is_string($name))
            {
                $this->drivers[$this->driver_count]->name = $name;
            }
            else
            {
                $this->drivers[$this->driver_count]->name = "New Driver";
                echo 'Driver name was set to default because "' . $name . '" is not a string.';
            }
            $this->driver_count++;
        }
        catch (Exception $ex)
        {
          echo $ex;
        }
    }

  /**
 * Entfernt einen Fahrer aus dem Team.
 *
 * @author Timo
 */
    public function RemoveDriver($name)
    {
        try
        {
          if(is_string($name))
          {
            $i = 0;
            foreach($this->drivers as $driver)
            {
                if($driver->GetName() == $name)
                {
                    unset($this->drivers[$i]);
                    $this->drivers = array_values($this->drivers);
                    $this->driver_count--;
                    return true;
                }
                $i++;
            }
            echo $name . ' is not an existing driver.';
            return false;
          }
          else
          {
              echo $name . ' is not a string.';
              return false;
          }
        }
        catch (Exception $ex)
        {
          echo $ex;
          return false;
        }
    }

    public function GetDriverCount()
    {
        return $this->driver_count;
    }

   public function GetDriver($index)
   {
       try
       {
         return $this->drivers[$index];
       }
       catch (Exception $ex)
       {
         return -1;
       }
   }

 /**
 * Gibt Fahrer über Namen zurück.
 * Über den exakten Namen oder sobald das Suchwort enthalten ist.
 * Gibt das erste Ergebnis zurück, welches darauf zutrifft.
 *
 * @author Timo
 */
    public function GetDriverByName($name, $exact_name = true)
    {
        if(is_string($name))
        {
            try
            {
                for($i = 0; $i < $this->driver_count; $i++)
                {
                    if($exact_name)
                    {
                        if($this->drivers[$i]->GetName() == $name)
                        {
                            return $this->drivers[$i];
                        }
                    }
                    else
                    {
                        if(stristr($this->drivers[$i]->GetName(), $name))
                        {
                            return $this->drivers[$i];
                        }
                    }
                }
                return null;
            }
            catch (Exception $ex)
            {
                echo $ex;
                return -1;
            }
        }
        else
        {
          echo $name . ' is not a string.';
        }
    }

   public function SetDriverIDs($ids)
   {
       if(is_array($ids))
       {
           $this->driver_ids = $ids;
       }
       else
       {
           echo $ids . ' is not an array.<br />';
       }
   }


   public function GetDriverIDs()
   {
       return $this->driver_ids;
   }

    public function SyncTeamWithDB($series_prefix)
    {
        require_once('Database/Database.php');

        $db        = new Database();
        $db->Connect();
        $condition = 'ID_Team = ' . $this->id;
        $data      = $db->Select($series_prefix . '_teams', $condition); // TODO: dynamische Tabelle
        $db->Disconnect();
        try
        {
            $this->active          = $data->Active;
            $this->id              = $data->ID_Team;
            $this->team_id         = $data->Team_ID;
            $this->points          = $data->Points;
            $this->mulligan        = $data->Mulligan;
            $this->name            = $data->Name;
            $this->penalty_points  = $data->Penalty_Points;
            $this->races           = $data->Races;
            $this->wins            = $data->Wins;
            $this->top5            = $data->Top5;
            $this->driver_count    = $data->Driver_Count;
        }
        catch(Exception $ex)
        {
            echo 'Syncing error: ' . $ex . '<br />';
        }
    }
}
?>
